import React, { Component } from "react";
import { Modal, Table, message, Input, Button } from "antd";

class ResultPopup extends Component {
  constructor(props) {
    super(props);
    this.state = {
      awardList: "",
      awardTotal: 0,
      current: 1,
      loading: false
    };
  }

  request = window.request;

  componentWillMount() {
    this.getRealy(1);
  }

  pageChange = page => {
    this.setState({ current: page });
    this.getRealy(page);
  };

  getRealy = page => {
    this.setState({ loading: true });
    this.request("/miner/recruit/list", {
      method: "POST",
      body: {
        currentPage: page,
        showCount: 6
      }
    }).then(json => {
      this.setState({ loading: false });
      if (json.code === 10000000) {
        let awardList = [];
        if (json.data.list) {
          awardList = json.data.list.map((item, index) => {
            item.key = index + 1;
            return item;
          });
        }
        this.setState({
          awardList: awardList,
          awardTotal: json.data.count
        });
      } else {
        message.error(json.msg);
      }
    });
  };

  getMyRealy = () => {
    this.setState({ loading: true });
    this.request("/miner/recruit/my", {
      method: "GET"
    }).then(json => {
      this.setState({ loading: false });
      if (json.code === 10000000) {
        let awardList = [];
        if (json.data.list) {
          awardList = json.data.list.map((item, index) => {
            item.key = index + 1;
            return item;
          });
        }

        this.setState({
          awardList: awardList,
          awardTotal: json.data.count
        });
      } else {
        message.error(json.msg);
      }
    });
  };

  allClick = () => {
    this.getRealy(1);
    this.setState({ currentPage: 1 });
  };

  searchClick = () => {
    const account = sessionStorage.getItem("account");
    if (account) {
      this.getMyRealy();
    } else {
      message.destroy();
      message.error("请先登录");
    }
  };

  render() {
    const { awardList, awardTotal, current, loading } = this.state;
    const { onCancel } = this.props;
    const awardColumns = [
      {
        align: "center",
        title: "账号",
        dataIndex: "mail",
        key: "mail"
      },
      {
        align: "center",
        title: "姓名",
        dataIndex: "realName",
        key: "realName"
      },
      {
        align: "center",
        title: "个人持币是否达标",
        dataIndex: "isStandard",
        key: "isStandard",
        render: text => {
          if (text === "1") {
            return <div style={{ color: "#d39f5e" }}>已达标</div>;
          } else {
            return <div>未达标</div>;
          }
        }
      },
      {
        align: "center",
        title: "直推人数是否达标",
        dataIndex: "isInviteNumber",
        key: "isInviteNumber",
        render: text => {
          if (text === "1") {
            return <div style={{ color: "#d39f5e" }}>已达标</div>;
          } else {
            return <div>未达标</div>;
          }
        }
      },
      {
        align: "center",
        title: "持币挖矿是否达标",
        dataIndex: "isReachNumber",
        key: "isReachNumber",
        render: text => {
          if (text === "1") {
            return <div style={{ color: "#d39f5e" }}>已达标</div>;
          } else {
            return <div>未达标</div>;
          }
        }
      }
    ];
    return (
      <Modal
        title="矿主招募结果"
        visible={true}
        wrapClassName="v-center-modal"
        width={800}
        footer={null}
        onCancel={onCancel}
      >
        <div
          style={{
            display: "flex",
            marginBottom: 12,
            justifyContent: "flex-end"
          }}
        >
          <Button type="primary" onClick={this.searchClick}>
            查看我的报名结果
          </Button>
          <Button
            type="primary"
            style={{ marginLeft: 20 }}
            onClick={this.allClick}
          >
            全部
          </Button>
        </div>
        <Table
          dataSource={awardList ? awardList : []}
          columns={awardColumns}
          loading={loading}
          pagination={{
            total: awardTotal,
            current,
            pageSize: 6,
            onChange: page => {
              this.pageChange(page);
            }
          }}
        />
      </Modal>
    );
  }
}

export default ResultPopup;
